<?php

namespace App\Sidong\Controllers;

use App\Models\MemberCardOrder;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class MemberCardOrderController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '会员卡订单';

    public function index(Content $content)
    {
        return $content
            ->title($this->title())
            ->breadcrumb(['text' => '会员卡订单列表'])
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new MemberCardOrder);
        $admin = \Admin::user();
        $grid->model()
            ->where('tenant_id', $admin->tenant_id)
            ->where('is_paid', true)
            ->orderByDesc('paid_time')
            ->paginate(10);
        $grid->disableBatchActions();
        $grid->disableExport();
        $grid->disableCreateButton();
        $grid->disableActions();
        $grid->quickSearch('order_sn', 'transaction_id', 'openid');

        $grid->column('id', 'ID');
        $grid->column('user.phone', '会员手机号');
        $grid->column('memberCard.name', '会员卡')->display(function($name) {
            $src = \Storage::disk(config('admin.upload.disk'))->url($this->memberCard->cover_url);
            return '<img  height="35" style="margin-right: 5px;" src="' . $src . '"/>' . $name;
        });
        $grid->column('order_sn', '订单编号')->copyable();
        $grid->column('transaction_id', '微信支付流水号')->copyable();
        $grid->column('openid', '用户微信OPENID')->copyable();
        $grid->column('total_fee', '支付金额')->display(function($total_fee) {
            return  '¥' . $total_fee;
        })->totalRow(function ($amount) {
            return "<span class='text-danger text-bold'><i class='fa fa-yen'></i> {$amount} 元</span>";
        });
        $grid->column('is_paid', '支付状态')->display(function($is_paid) {
            return $is_paid ? '<span class="label label-success">已支付</span>' : '<span class="label label-danger">未支付</span>';
        });
        $grid->column('paid_time', '支付时间');
        $grid->footer(function ($query) {
            // 查询出已支付状态的订单总金额
            $data = $query->where('is_paid', 1)->sum('total_fee');
            return "<div style='padding: 10px;'>总收入：¥$data</div>";
        });
        return $grid;
    }

}
